package com.project.oauth2;

import com.project.dao.UserDao;
import com.project.entity.UserEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Component
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {


    @Resource
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(username);
        UserEntity findUser = userDao.findOne(userEntity);
        if (ObjectUtils.isEmpty(findUser)) {
            throw new UsernameNotFoundException("用户未找到");
        } else {
            return new com.project.oauth2.UserDetails(findUser);
        }
    }
}
